package com.itwc.websocketdemo.mapper;

import com.itwc.websocketdemo.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户信息表 数据层
 */
@Mapper
public interface SysUserMapper {
    
    /**
     * 根据用户名查询用户
     */
    @Select("SELECT * FROM sys_user WHERE user_name = #{userName} AND del_flag = '0'")
    SysUser selectUserByUserName(@Param("userName") String userName);
    
    /**
     * 根据用户ID查询用户
     */
    @Select("SELECT * FROM sys_user WHERE user_id = #{userId} AND del_flag = '0'")
    SysUser selectUserById(@Param("userId") Long userId);
    
    /**
     * 查询所有正常状态的用户
     */
    @Select("SELECT * FROM sys_user WHERE status = '0' AND del_flag = '0'")
    List<SysUser> selectAllActiveUsers();
    
    /**
     * 更新用户最后登录信息
     */
    @Update("UPDATE sys_user SET login_ip = #{loginIp}, login_date = #{loginDate} WHERE user_id = #{userId}")
    int updateUserLoginInfo(@Param("userId") Long userId, @Param("loginIp") String loginIp, @Param("loginDate") LocalDateTime loginDate);
}